package com.kevin.table;

import com.kevin.enums.DBType;
import com.kevin.sql.DataType;

public class MCellStruct {
	
    public MCellStruct(DBType dbType)
    {
    	this.dbType = dbType;                                                                                                                          
    }
    public MCellStruct(String columnName, int sqlType)
    {
        Init(columnName, sqlType, false, true, false, -1, null);
    }
    public MCellStruct(String columnName, int sqlType, boolean isAutoIncrement, boolean isCanNull, int maxSize)
    {
        Init(columnName, sqlType, isAutoIncrement, isCanNull, false, maxSize, null);
    }
	
    public void Init(String columnName, int sqlType,boolean isAutoIncrement, boolean isCanNull, boolean isPrimaryKey, int maxSize, Object defaultValue)
    {
        this.columnName = columnName.trim();
        this.setSqlType(sqlType);
        this.autoIncrement = isAutoIncrement;
        this.canNull = isCanNull;
        this.maxSize = maxSize;
        this.primaryKey = isPrimaryKey;
        this.defaultValue = defaultValue;
    }
	
    /**
     * 原始的数据库字段类型名称
     */
    public String typeName;
    
    /**
     * 数据库字段类型
     */
    private int sqlType;
    
    /**
     * 值类型
     */
    Class<?> valueType;
	
	/**
	 * 是否关键字
	 */
	private boolean primaryKey;
	
	/**
	 * 是否唯一索引
	 */
	private boolean uniqueKey;
	
	/**
	 * 是否外键
	 */
	private boolean foreignKey= false;
	
	/**
	 * 外键表名
	 */
	private String fkTableName;
	
	/**
	 * 字段描述
	 */
	private String description;
	
	/**
	 * 默认值（未完成）
	 */
	private Object defaultValue;
	
	/**
	 * 是否允许为Null
	 */
	private boolean canNull;
		
	/**
	 * 是否自增加
	 */
	private boolean autoIncrement;
	
	/**
	 * 旧的列名（AlterOp为Rename时可用）
	 */
	public String oldName;

	/**
	 * 列名（未完成）
	 */
	private String columnName;
	
	/**
	 * 表名
	 */
	private String tableName;

	/**
	 * 最大字节
	 */
	private int maxSize;
	
	/**
	 * 精度（小数位）
	 */
	private int scale;
	
	/**
	 * 内部使用的索引，在字段名为空时使用
	 */
	//private int readerIndex;
	
	/**
	 * 数据库类型
	 */
	private DBType dbType = DBType.NONE;
	
	/**
	 * 结构集合
	 */
	private transient MDataColumn mDataColumn;

	private Object alterOp;

	
	public MCellStruct clone()
    {
        MCellStruct ms = new MCellStruct(dbType);
        ms.setColumnName(this.getColumnName());
        ms.setSqlType(this.getSqlType());
        ms.autoIncrement = autoIncrement;
        ms.canNull = canNull;
        ms.maxSize = maxSize;
        ms.scale = scale;
        ms.primaryKey = primaryKey;
        ms.uniqueKey = uniqueKey;
        ms.foreignKey = foreignKey;
        ms.fkTableName = fkTableName;
        ms.typeName = typeName;
        ms.setDefaultValue(this.getDefaultValue());
        ms.description = description;
        ms.setmDataColumn(this.getmDataColumn());
        ms.setAlterOp(this.getAlterOp());
        ms.setTableName(this.getTableName());
        return ms;
    }
	

	public int getScale() {
		return scale;
	}
	public String getTypeName() {
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	public Object getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(Object defaultValue) {
		this.defaultValue = defaultValue;
	}
	public String getColumnName() {
		return columnName;
	}
	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public int getSqlType() {
		return sqlType;
	}
	public void setSqlType(int sqlType) {
		this.sqlType = sqlType;
		valueType = DataType.getType(sqlType, getDbType());
	}
	
	public Class<?> getValueType() {
		return valueType;
	}

	public DBType getDbType() {
		if(mDataColumn != null){
			return mDataColumn.dbType;
		}
		return dbType;
	}
	
	public MDataColumn getmDataColumn() {
		return mDataColumn;
	}

	public void setmDataColumn(MDataColumn mDataColumn) {
		this.mDataColumn = mDataColumn;
	}
	public Object getAlterOp() {
		return alterOp;
	}
	public void setAlterOp(Object alterOp) {
		this.alterOp = alterOp;
	}
	public void setFkTableName(String fkTableName) {
		this.fkTableName = fkTableName;
	}
	public String getFkTableName() {
		return fkTableName;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getMaxSize() {
		return maxSize;
	}
	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}
	public void setScale(int scale) {
		this.scale = scale;
	}
	public boolean isCanNull() {
		return canNull;
	}
	public void setCanNull(boolean canNull) {
		this.canNull = canNull;
	}
	public boolean isAutoIncrement() {
		return autoIncrement;
	}
	public void setAutoIncrement(boolean autoIncrement) {
		this.autoIncrement = autoIncrement;
	}
	public boolean isPrimaryKey() {
		return primaryKey;
	}
	public void setPrimaryKey(boolean primaryKey) {
		this.primaryKey = primaryKey;
	}
	public boolean isUniqueKey() {
		return uniqueKey;
	}
	public void setUniqueKey(boolean uniqueKey) {
		this.uniqueKey = uniqueKey;
	}
	public boolean isForeignKey() {
		return foreignKey;
	}
	public void setForeignKey(boolean foreignKey) {
		this.foreignKey = foreignKey;
	}	
	
}
